import { InsertResult, Repository } from "typeorm";
import { DbConfig } from "../../src/config/DbConfig";
import { GameConfig } from "../../src/config/GameConfig";
import { RoomConfig } from "../../src/config/RoomConfig";
import { DaoBase } from "../../src/engine/dao/DaoBase";
import { DBSourceManager } from "../../src/engine/utils/DBSourceManager";
import { Logger } from "../../src/engine/utils/Logger";
import { MysqlClient } from "../../src/engine/utils/MySqlClient";
import { MysqlManager } from "../../src/engine/utils/MysqlManager";
import { JhaoProto } from "../../src/lib/net/protobuf/jhaoproto";
import { RoomTemplateEntity } from "../entity/RoomTemplateEntity";

export class RoomTemplateDao extends DaoBase {

    public static DB_NAME: string = DbConfig.Db_Name.db_admin;
    public static Tb_Name: string = "t_room_template";

    public static async getRepository() {
        let repository: Repository<RoomTemplateEntity> = await this.getRepositoryBase(RoomTemplateEntity, this.getGameDbName())
        return repository;
    }



    public static async insert_room_template(userid: number, protoPack: JhaoProto.req_club_create_room_template) {
        let entity: RoomTemplateEntity = new RoomTemplateEntity()
        entity.clubid = protoPack.clubid
        entity.creator = userid
        entity.game_type = protoPack.gameType
        entity.name = protoPack.name
        entity.roomType = protoPack.roomType
        entity.conf_base = JSON.stringify(protoPack.roomConfigBase)
        if (entity.game_type == GameConfig.Game_Type.Mj) {
            entity.config_game = JSON.stringify(protoPack.roomConfigMj)
        }
        let repository: Repository<RoomTemplateEntity> = await this.getRepository()
        let result: InsertResult = await DBSourceManager.instance.insert(repository, entity)
        if (result && result.raw["affectedRows"] > 0) {
            return true
        }
        return false
    }

    public static async get_all_room_template() {
        let sql: string = "select * from  t_room_template ;"
        let repository: Repository<RoomTemplateEntity> = await this.getRepository()
        let results = await repository.query(sql)
        if (results && results.length) {
            return results
        }
        return []
    }

    public static async delete_room_template(id: number) {
        let sql: string = "delete from t_room_template where id=?;"
        let sqlParams = [id];
        let client: MysqlClient = await MysqlManager.instance.getDb(this.getGameDbName());
        let rows = await client.queryPromise(sql, sqlParams);
        Logger.log("delete_room_template==", sql, sqlParams, rows)
        if (rows && rows["affectedRows"] > 0) {
            return true
        } else {
            return false
        }
    }

    public static async get_ziyou_room_template(gameType: number) {
        let sql: string = "select * from  t_room_template  where `game_type`=? and `roomType`=?;"
        let sqlParams = [gameType, RoomConfig.Room_Type.Ziyou];
        let client: MysqlClient = await MysqlManager.instance.getDb(this.getGameDbName());
        let rows = await client.queryPromise(sql, sqlParams);
        if (rows && rows.length > 0) {
            return rows
        }
        return []
    }


}